﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using quanlynhasach;

namespace quanlynhasach.Linksql
{
    class Nhanviensql
    {
        Connection db = new Connection();
        Nhanvien nv = new Nhanvien();

        public String login(string user, string pw)
        {
            string query;
            string manv = "";
            query = "Select * from NhanVien where MaNV = '" + user + "'" + " and PassWord = '" + pw + "'";
            if (db.getDS(query).Rows.Count == 1)
                manv = db.getDS(query).Rows[0]["MaNV"].ToString();
            return manv;
        }

        public bool quyen(string user)
        {
            if (db.getDS("Select Loai from NhanVien where MaNV = '" + user + "'").Rows[0]["Loai"].ToString() == "Quan ly")
                return true;
            return false;
        }

        public DataTable getNV(string manv)
        {
            return db.getDS("Select * from NhanVien where MaNV = '" + manv + "'");
        }

        public bool DoiMK(string pw)
        {
            string[] param = { "@MaNV", "@PassWord" };
            object[] value = { frmMain.user, pw };
            string query = "Update NhanVien"
                           + " set PassWord=@PassWord"
                           + " where MaNV=@MaNV";
            return db.ExecuteNonQueryPara(query, param, value);
        }
        public DataTable getNV()
        {
            return db.getDS("Select MaNV,TenNV,NgaySinh,GioiTinh,DiaChi,SoDienThoai,SoCMND,Email,Loai,Anh from NhanVien ");
        }

        public bool check(string manv)
        {
            if (db.getDS("Select * from NhanVien where MaNV = '"+manv+"'").Rows.Count > 0)
                return true;
            return false;
        }
        public bool XoaNV(string manv)
        {
            string[] param = { "@MaNV" };
            object[] value = { manv };
            string query = "Delete from NhanVien where MaNV=@MaNV";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool SuaNV(Nhanvien nv)
        {
            string[] param ={"@MaNV","@TenNV","@NgaySinh","@GioiTinh","@DiaChi",
                           "@SoDienThoai","@SoCMND","@Email", "@PassWord", "@Loai", "@Anh"};
            object[] value = {nv.MaNV, nv.TenNV, nv.NgaySinh, nv.GioiTinh, nv.DiaChi, nv.SoDienThoai, nv.SoCMND,
                             nv.Email, nv.PassWord, nv.Loai, nv.Anh };
            string query = "Update NhanVien"
                           + " set TenNV=@TenNV,NgaySinh=@NgaySinh,GioiTinh=@GioiTinh,DiaChi=@DiaChi,"
                           + "SoDienThoai=@SoDienThoai,SoCMND=@SoCMND,Email=@Email, PassWord=@PassWord, Loai=@Loai, Anh=@Anh"
                           + " where MaNV=@MaNV";
            return db.ExecuteNonQueryPara(query, param, value);
        }

        public bool ThemNV(Nhanvien nv)
        {
            string[] param ={"@MaNV","@TenNV","@NgaySinh","@GioiTinh","@DiaChi",
                           "@SoDienThoai","@SoCMND","@Email", "@PassWord", "@Loai", "@Anh"};
            object[] value = {nv.MaNV, nv.TenNV, nv.NgaySinh, nv.GioiTinh, nv.DiaChi, nv.SoDienThoai, nv.SoCMND,
                             nv.Email, nv.PassWord, nv.Loai, nv.Anh };
            string query = "Insert into NhanVien"
                           + "(MaNV,TenNV,NgaySinh,GioiTinh,DiaChi,SoDienThoai,SoCMND,Email,PassWord,Loai,Anh)"
                           + " values (@MaNV,@TenNV,@NgaySinh,@GioiTinh,@DiaChi,@SoDienThoai,@SoCMND,@Email,@PassWord,@Loai,@Anh)";
            return db.ExecuteNonQueryPara(query, param, value);
        }
    }
}
